package main;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class CreateData {

	static JdbcTemplate template;
	
	public static void main(String[] args) {
		ApplicationContext ac = new FileSystemXmlApplicationContext("/src/main/resources/beans_jdbc.xml");
		template=(JdbcTemplate) ac.getBean("jdbcTemplate");
		
		String sql="select * from test where id=1";
        Map map=template.queryForMap(sql);
//        List list=template.queryForList(sql);
        String a=(String) map.get("a");
        for(int i=0;i<10;i++){
        	
        	map.put("id", i);
        	
        	map.put("a", a+i);
        	insert("test",map);
            
            
        }
        
        
	}

	private static void insert(String table,Map<String, Object> map) {
		String sql = "INSERT INTO " + table;
		Iterator<String> keySet=map.keySet().iterator();
		List<Object> parameters=new ArrayList<Object>();
		String keys="(";
		String values="(";
		while (keySet.hasNext()) {
			String key=keySet.next();
			keys+=key+",";
			values+="?,";
			parameters.add(map.get(key));
		}
		keys=keys.substring(0, keys.lastIndexOf(","))+")";
		values=values.substring(0, values.lastIndexOf(","))+")";
		sql+=keys+" values "+values;
		
		template.update(sql, parameters.toArray());
	}

}
